package com.site.action.master;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.site.dto.master.CodeBizBean;
import com.site.service.master.CodeService;
import com.site.util.Code;
import com.site.util.MessageUtils;
import com.site.util.StringUtils;
import com.site.util.base.action.BackendBaseAction;
import com.site.util.base.service.BaseService;

/**
 * 编码管理
 * @author Lee 
 *
 */
public class CodeAction extends BackendBaseAction {
	
	private static final long serialVersionUID = 1L;

	private CodeService codeService;
	
	private CodeBizBean codeBizBean;
	
	private List<CodeBizBean> codeBizBeans;
	/*
	 *编码类型名称
	 */
	private List<String> codeNameList; 
	/*
	 * 编码备注
	 */
	private List<String> codeRemarkList;
	/*
	 * 编码添加方式
	 */
	private List<Code> codeTypeList;
	/*
	 * 编码类型的值(用于：增加)
	 */
	private Integer codeType=1;
	/*
	 * 编码名称
	 */
	private String codeName;
	/*
	 * 出错后返回增加页面的常量
	 */
	public final static String ADD="getCodeAdd";
	/*
	 * 旧编码名称（用于：修改页面）
	 */
	private String oldCodeName;
	/*
	 * 旧编码值（用于：修改页面）
	 */
	private String oldCodeKey;
	/**
	 * 获得所有编码数据
	 * @return
	 */
	public String getCodeList(){
		if(null == queryPage){
			initPagination();
		}
		codeBizBeans = codeService.getAllCodeBizBean(queryPage, codeBizBean);
		init();
		codeRemarkList = codeService.findAllCode();
		return "getCodeList";
	}
	
	/**
	 * 初始化
	 */
	private void init(){
		codeNameList = codeService.getAllCodeName();
		if(null == codeNameList || codeNameList.isEmpty()){
			codeNameList = new ArrayList<String>();
		}
		codeTypeList = MessageUtils.getCodeList("code_type", request);
	}
	/**
	 * 更新排序
	 * @return
	 */
	public String updateCodeOrderBy() {
		if(null != codeBizBeans && codeBizBeans.size() > 0){
			for (CodeBizBean codeBizBean : codeBizBeans) {
				if(null != codeBizBean && codeBizBean.getCodeId() != null){
					codeBizBean.setOrderBy(this.codeBizBean.getOrderBy());
					codeBizBean.setUpdatedUser(getCurrentUser().getUserName());
					codeService.execute(codeBizBean, BaseService.COMMAND_UPDATE);
					break;
				}
			}
			if(queryPage==null){
				initPagination();
			}
			codeBizBeans = codeService.getAllCodeBizBean(queryPage,codeBizBean);
			init();
		}
		
		return "getCodeList";
	}
	/**
	 * 增加编码页面
	 * @return
	 */
	public String getCodeAdd(){
		init();
		return "getCodeAdd";
	}
	/**
	 * 增加编码
	 * @return
	 */
	public String codeAdd(){
		if(checkAddCode()){
			init();
			return ADD;
		}
		codeBizBean.setOrderBy(1);
		if(codeType==1)
			codeBizBean.setCodeName(codeName);
		try{
			codeBizBean.setCreatedUser(getCurrentUser().getUserName());
			codeBizBean.setUpdatedUser(getCurrentUser().getUserName());
			codeService.execute(codeBizBean, BaseService.COMMAND_SAVE);
			super.setMessageID("SYS_MSG_00022");
		} catch(Exception e) {
			init();
			addActionError("添加失败！");
			return ADD;
		}
		super.setBackurl(request.getContextPath() + "/code/codeAction!getCodeList.do");
		return "toMessage";
	}
	/**
	 * 添加编码验证
	 * @return
	 */
	public boolean checkAddCode(){
		boolean isError = false;
		if(StringUtils.isEmpty(codeBizBean.getCodeKey())){
			super.addFieldError("codeKey","编码键名不能为空！");
			isError=true;
		}
		
		if(StringUtils.isEmpty(codeBizBean.getCodeValue())){
			super.addFieldError("codeValue","编码键值不能为空！");
			isError=true;
		}
		if(StringUtils.isEmpty(codeBizBean.getRemark())){
			super.addFieldError("remark","编码备注不能为空！");
			isError=true;
		}
		
		if(codeType==1){
			if(StringUtils.isEmpty(codeName)){
				super.addFieldError("codeName","编码名称不能为空！");
				isError=true;
			} else {
				boolean isExists = codeService.isExistsCodeNameOrCodeKey(codeName, codeBizBean.getCodeKey(), 1);
				if(isExists){
					super.addFieldError("codeName","编码名称已存在！");
					isError=true;
				}
			}
			return isError;
		}else{
			if(StringUtils.isEmpty(codeBizBean.getCodeName())){
				super.addFieldError("codeName","暂无添加编码！");
				isError=true;
			}else{
				
				boolean isExists = codeService.isExistsCodeNameOrCodeKey(codeBizBean.getCodeName(), codeBizBean.getCodeKey(), 2);
				if(isExists){
					super.addFieldError("codeKey","编码键名已存在！");
					isError=true;
				}
			}
			return isError;
		}
		
	}
	/**
	 * 编码修改页面
	 * @return
	 */
	public String getCodeEdit(){
		if(null != codeBizBeans && codeBizBeans.size() > 0){
			for (CodeBizBean codeBizBean : codeBizBeans) {
				if(null != codeBizBean && codeBizBean.getCodeId() != null){
					this.codeBizBean = codeService.getCodeById(codeBizBean.getCodeId());
					oldCodeName = codeBizBean.getCodeName();
					oldCodeKey = codeBizBean.getCodeKey();
					break;
				}
			}
		}
		return "getCodeEdit";
	}
	/**
	 * 编码修改
	 * @return
	 */
	public String codeEdit(){
		if(null != codeBizBean){
			if(checkUpdateCode())
				return "getCodeEdit";
			codeService.execute(codeBizBean,BaseService.COMMAND_UPDATE);
			super.setMessageID("SYS_MSG_00022");
		} else {
			super.setMessageID("SYS_MSG_00026");
		}
		super.setBackurl(request.getContextPath() + "/code/codeAction!getCodeList.do");
		return "toMessage";
	}
	/**
	 * 检查修改编码
	 * @return
	 */
	public boolean checkUpdateCode(){
		boolean isError = false;
		if(StringUtils.isEmpty(codeBizBean.getCodeName())){
			super.addFieldError("codeName","编码名称不能为空！");
			isError=true;
		}
		if(StringUtils.isEmpty(codeBizBean.getCodeKey())){
			super.addFieldError("codeKey","编码键名不能为空！");
			isError=true;
		}
		
		if(StringUtils.isEmpty(codeBizBean.getCodeValue())){
			super.addFieldError("codeValue","编码键值不能为空！");
			isError=true;
		}
		if(StringUtils.isEmpty(codeBizBean.getRemark())){
			super.addFieldError("remark","编码备注不能为空！");
			isError=true;
		}
		if(!codeBizBean.getCodeName().equals(oldCodeName)){
			boolean isExistsCodeName = codeService.isExistsCodeNameOrCodeKey(codeBizBean.getCodeName(),codeBizBean.getCodeKey(), 2);
			if(isExistsCodeName){
				super.addFieldError("codeName","编码名称已存在！");
				isError=true;
			}
		}
		if(!codeBizBean.getCodeKey().equals(oldCodeKey)){
			boolean isExistsCodeKey = codeService.isExistsCodeNameOrCodeKey(codeBizBean.getCodeName(),codeBizBean.getCodeKey(), 2);
			if(isExistsCodeKey){
				super.addFieldError("codeKey","编码键名已存在！");
				isError=true;
			}
		}
		return isError;
	}
	/**
	 *  删除编码
	 * @return
	 */
	public String codeDelete(){
		if(null != codeBizBeans && codeBizBeans.size() > 0){
			codeService.execute(codeBizBeans,BaseService.COMMAND_DELETE_ALL);
			super.setMessageID("SYS_MSG_00022");
		} else {
			super.setMessageID("SYS_MSG_00026");
		}
		super.setBackurl(request.getContextPath() + "/code/codeAction!getCodeList.do");
		return "toMessage";
	}
	
	public CodeService getCodeService() {
		return codeService;
	}
	@Resource
	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public CodeBizBean getCodeBizBean() {
		return codeBizBean;
	}

	public void setCodeBizBean(CodeBizBean codeBizBean) {
		this.codeBizBean = codeBizBean;
	}

	public List<CodeBizBean> getCodeBizBeans() {
		return codeBizBeans;
	}

	public void setCodeBizBeans(List<CodeBizBean> codeBizBeans) {
		this.codeBizBeans = codeBizBeans;
	}

	public List<String> getCodeNameList() {
		return codeNameList;
	}

	public void setCodeNameList(List<String> codeNameList) {
		this.codeNameList = codeNameList;
	}

	public List<String> getCodeRemarkList() {
		return codeRemarkList;
	}

	public void setCodeRemarkList(List<String> codeRemarkList) {
		this.codeRemarkList = codeRemarkList;
	}

	public List<Code> getCodeTypeList() {
		return codeTypeList;
	}

	public void setCodeTypeList(List<Code> codeTypeList) {
		this.codeTypeList = codeTypeList;
	}

	public Integer getCodeType() {
		return codeType;
	}

	public void setCodeType(Integer codeType) {
		this.codeType = codeType;
	}

	public String getCodeName() {
		return codeName;
	}

	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}

	public String getOldCodeName() {
		return oldCodeName;
	}

	public void setOldCodeName(String oldCodeName) {
		this.oldCodeName = oldCodeName;
	}

	public String getOldCodeKey() {
		return oldCodeKey;
	}

	public void setOldCodeKey(String oldCodeKey) {
		this.oldCodeKey = oldCodeKey;
	}

	
}
